package cn.hy.config;

import cn.hy.enums.OrderStatusEnum;
import cn.hy.events.OrderStatusChangeEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.EnumSet;

@Configuration
@EnableStateMachine(name = "orderStatusMachine")
public class OrderStatusMachineConfig extends StateMachineConfigurerAdapter<OrderStatusEnum, OrderStatusChangeEvent> {

    /**
     * 配置状态
     *
     * @param orderStatusMachineConfig：
     * @throws Exception：
     */
    public void configure(StateMachineStateConfigurer<OrderStatusEnum, OrderStatusChangeEvent> orderStatusMachineConfig) throws Exception {
        orderStatusMachineConfig.withStates()
                .initial(OrderStatusEnum.PAY_WAIT)
                .states(EnumSet.allOf(OrderStatusEnum.class));
    }


    /**
     * 配置状态转换事件关系
     *
     * @param orderStatusMachineConfig：
     * @throws Exception：
     */
    public void configure(StateMachineTransitionConfigurer<OrderStatusEnum, OrderStatusChangeEvent> orderStatusMachineConfig) throws Exception {
        orderStatusMachineConfig
//            待付款 -> 部分支付 部分支付事件
                .withExternal().source(OrderStatusEnum.PAY_WAIT).target(OrderStatusEnum.PAY_PART).event(OrderStatusChangeEvent.PAY_WAIT_TO_PAY_PART_EVENT)
                .and()
//            待付款 -> 已支付(待成团) 全部支付，待成团
                .withExternal().source(OrderStatusEnum.PAY_WAIT).target(OrderStatusEnum.PAY_OVER).event(OrderStatusChangeEvent.PAY_WAIT_TO_PAY_OVER_EVENT)
                .and()
//            待付款 -> 待发货(待授权) 实体全部付款待发货，虚拟全部付款待授权
                .withExternal().source(OrderStatusEnum.PAY_WAIT).target(OrderStatusEnum.SEND_WAIT).event(OrderStatusChangeEvent.PAY_WAIT_TO_SEND_WAIT_EVENT)
                .and()
//            待付款 -> 已完成(全部收货) 不需要发货
                .withExternal().source(OrderStatusEnum.PAY_WAIT).target(OrderStatusEnum.FINISHED).event(OrderStatusChangeEvent.PAY_WAIT_TO_FINISHED_EVENT)
                .and()
//            待付款 -> 已关闭 订单取消
                .withExternal().source(OrderStatusEnum.PAY_WAIT).target(OrderStatusEnum.CLOSED).event(OrderStatusChangeEvent.PAY_WAIT_TO_CLOSED_EVENT)
                .and()

//            部分支付 -> 已支付(待成团) 拼团订单支付剩下的一部分
                .withExternal().source(OrderStatusEnum.PAY_PART).target(OrderStatusEnum.PAY_OVER).event(OrderStatusChangeEvent.PAY_PART_TO_PAY_OVER_EVENT)
                .and()
//            部分支付 -> 待发货(待授权) 订单支付剩下的一部分
                .withExternal().source(OrderStatusEnum.PAY_PART).target(OrderStatusEnum.SEND_WAIT).event(OrderStatusChangeEvent.PAY_PART_TO_SEND_WAIT_EVENT)
                .and()
//            部分支付 -> 已完成(全部收货) 不需要发货订单支付剩下的一部分
                .withExternal().source(OrderStatusEnum.PAY_PART).target(OrderStatusEnum.FINISHED).event(OrderStatusChangeEvent.PAY_PART_TO_FINISHED_EVENT)
                .and()

//            已支付(待成团) -> 待发货(待授权) 拼团订单拼团成功
                .withExternal().source(OrderStatusEnum.PAY_OVER).target(OrderStatusEnum.SEND_WAIT).event(OrderStatusChangeEvent.PAY_OVER_TO_SEND_WAIT_EVENT)
                .and()
//            已支付(待成团) -> 已完成(全部收货) 不需要发货的订单拼团成功
                .withExternal().source(OrderStatusEnum.PAY_OVER).target(OrderStatusEnum.FINISHED).event(OrderStatusChangeEvent.PAY_OVER_TO_FINISHED_EVENT)
                .and()
//            已支付(待成团) -> 发起售后申请 拼团订单未拼成功发起售后申请
                .withExternal().source(OrderStatusEnum.PAY_OVER).target(OrderStatusEnum.AFTERMARKET_APPLY).event(OrderStatusChangeEvent.PAY_OVER_TO_AFTERMARKET_APPLY_EVENT)
                .and()

//            待发货(待授权) -> 部分发货
                .withExternal().source(OrderStatusEnum.SEND_WAIT).target(OrderStatusEnum.SEND_PART).event(OrderStatusChangeEvent.SEND_WAIT_TO_SEND_PART_EVENT)
                .and()
//            待发货(待授权) -> 已发货(全部发货)
                .withExternal().source(OrderStatusEnum.SEND_WAIT).target(OrderStatusEnum.SEND_OVER).event(OrderStatusChangeEvent.SEND_WAIT_TO_SEND_OVER_EVENT)
                .and()
//            待发货(待授权) -> 发起售后申请
                .withExternal().source(OrderStatusEnum.SEND_WAIT).target(OrderStatusEnum.AFTERMARKET_APPLY).event(OrderStatusChangeEvent.SEND_WAIT_TO_AFTERMARKET_APPLY_EVENT)
                .and()

//            部分发货 -> 已发货(全部发货)
                .withExternal().source(OrderStatusEnum.SEND_PART).target(OrderStatusEnum.SEND_OVER).event(OrderStatusChangeEvent.SEND_PART_TO_SEND_OVER_EVENT)
                .and()
//            部分发货 -> 发起售后申请
                .withExternal().source(OrderStatusEnum.SEND_PART).target(OrderStatusEnum.AFTERMARKET_APPLY).event(OrderStatusChangeEvent.SEND_PART_TO_AFTERMARKET_APPLY_EVENT)
                .and()

//            已发货(全部发货) -> 已完成(全部收货)
                .withExternal().source(OrderStatusEnum.SEND_OVER).target(OrderStatusEnum.FINISHED).event(OrderStatusChangeEvent.SEND_OVER_TO_FINISHED_EVENT)
                .and()
//            已发货(全部发货) -> 发起售后申请
                .withExternal().source(OrderStatusEnum.SEND_OVER).target(OrderStatusEnum.AFTERMARKET_APPLY).event(OrderStatusChangeEvent.SEND_OVER_TO_AFTERMARKET_APPLY_EVENT)
                .and()

//            已完成(全部收货) -> 发起售后申请
                .withExternal().source(OrderStatusEnum.FINISHED).target(OrderStatusEnum.AFTERMARKET_APPLY).event(OrderStatusChangeEvent.FINISHED_TO_AFTERMARKET_APPLY_EVENT)
                .and()

//            发起售后申请 -> 部分退款
                .withExternal().source(OrderStatusEnum.AFTERMARKET_APPLY).target(OrderStatusEnum.REFUND_PART).event(OrderStatusChangeEvent.AFTERMARKET_APPLY_TO_REFUND_PART_EVENT)
                .and()
//            发起售后申请 -> 已关闭
                .withExternal().source(OrderStatusEnum.AFTERMARKET_APPLY).target(OrderStatusEnum.CLOSED).event(OrderStatusChangeEvent.AFTERMARKET_APPLY_TO_CLOSED_EVENT)
                .and()

//            部分退款 -> 已关闭
                .withExternal().source(OrderStatusEnum.REFUND_PART).target(OrderStatusEnum.CLOSED).event(OrderStatusChangeEvent.REFUND_PART_TO_CLOSED_EVENT)
                .and()
//            部分退款 -> 发起售后申请 再次发起售后申请
                .withExternal().source(OrderStatusEnum.REFUND_PART).target(OrderStatusEnum.AFTERMARKET_APPLY).event(OrderStatusChangeEvent.REFUND_PART_TO_AFTERMARKET_APPLY_EVENT)
        ;
    }

}
